/*
 * NB-IOT.c
 *
 *  Created on: Jul 19, 2024
 *      Author: yuxuan
 */


#include <NB_IOT.h>
#include "usart.h"
#include <string.h>
#include <stdio.h>
#include "bsp_uart.h"
#include "crc16.h"


#if NB_IOT
//基本参数设置
/*
 * 	uint8_t model;				//透传云模式使能
	uint8_t device_ID;			//设备ID
	uint8_t device_password;	//设备密码
	uint8_t channel;			//通道号
*/


//设备参数初始化(具体设备参数见NB_IOT.h定义)
_NB_IOT NB_DTU=
{
	.work_model = NB_work_model,			//塔石DTU云连接
	.model = NB_model,       				//透传云模式使能
	.device_ID = NB_device_ID, 				//设备ID
	.device_password = NB_device_password,	//设备密码
	.channel = NB_channel					//通道号
};


//NB参数配置
void NB_configuration(void)
{
	uint8_t sendbuf[50]; 				//AT指令缓存
	char*config_cmd = "+++";  			//进入配置指令
	char*save_cmd = "AT&W";   			//保存设置指令
	char*restart_cmd = "AT+CFUN=1,1";   //重启设备

	//先进入配置模式
	Send_Data(&huart4,(uint8_t*)config_cmd,strlen(config_cmd));//正常来说响应OK才能进行配置，现在先不写  不知道怎么顺逻辑
	//NB_AT_response();
	//配置工作模式
	sprintf((char*)sendbuf,"AT+DTUMODE=%d,%d",NB_DTU.work_model,NB_DTU.channel);
	Send_Data(&huart4,(uint8_t*)sendbuf,strlen(sendbuf));
	//NB_AT_response();
	//塔石云设置
	sprintf((char*)sendbuf,"AT+DTUCLOUD=%d,\"%lu\",\"%lu\",%d",NB_DTU.model,NB_DTU.device_ID,NB_DTU.device_password,NB_DTU.channel);
	Send_Data(&huart4,(uint8_t*)sendbuf,strlen(sendbuf));
	//NB_AT_response();
	//保存设置
	Send_Data(&huart4,(uint8_t*)save_cmd,strlen(save_cmd));
	//重启设备
	Send_Data(&huart4,(uint8_t*)restart_cmd,strlen(restart_cmd));

}




//AT指令响应函数
//void NB_AT_response(void)
//{
//	uint32_t rxIndex = 0;
//	if(nbiot_rx_flag)//收到数据的标志位
//	{
//		nbiot_rx_flag = 0;//清除标志位
//		rxIndex = 0;
//		if(strstr((char *)nbiot_rx_buffer, "OK") != NULL)
//		{
//			//收到成功
//		}else
//		{
//			//Error_Handler();//未收到ok
//		}
//	}else
//	{
//		if(rxIndex<60000)
//		{
//			while(1)
//			{
//				rxIndex++;
//			}
//		}else
//		{
//			printf("error");
//		}
//	}
//
//}


/*
 * 读取振动传感器指令
 */
uint8_t VB_collect_step = 6;
uint8_t VB_collect_send_step = 1;
void NB_send(void)
{
	if(VB_collect_step==0)
	{
		VB_collect_step = 6;
		VB_collect_send_step = 1;
	}

	if(VB_collect_step)
	{
		if((connent_rx_flag == 1)&&(timer_count<60))
//		if((nbiot_rx_flag == 1)&&(nbiot_rx_len>6)&&(timer_count<60))
		{
			timer_count = 0;//停止计时
			NB_receivedata_CRC(nbiot_rx_buffer,nbiot_rx_len);
			connent_rx_flag =0;
			//nbiot_rx_flag = 0;
			VB_collect_step--;
			VB_collect_send_step = 1;
		}
		else if(timer_count>60)
		{
			VB_collect_step--;
			VB_collect_send_step = 1;
		}
	}
	if(VB_collect_send_step)
	{
		if(VB_collect_send_step)
		{
			switch (VB_collect_step)
			{
				case 0x06://读取三轴加速度指令
				{
					uint8_t VB_cmd[]={0x50,0x03,0x00,0x34,0x00,0x03,0x49,0x84};
					Send_Data(&huart4,(uint8_t*)VB_cmd,sizeof(VB_cmd));
//					Send_Data(&huart3,(uint8_t*)VB_cmd,sizeof(VB_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x05://角速度
				{
					uint8_t VB_cmd[]={0x50,0x03,0x00,0x37,0x00,0x03,0xB9,0x84};
					Send_Data(&huart4,(uint8_t*)VB_cmd,sizeof(VB_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x04://振动速度
				{
					uint8_t VB_cmd[]={0x50,0x03,0x00,0x3A,0x00,0x03,0x28,0x47};
					Send_Data(&huart4,(uint8_t*)VB_cmd,sizeof(VB_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x03://振动角度
				{
					uint8_t VB_cmd[]={0x50,0x03,0x00,0x3D,0x00,0x03,0x99,0x86};
					Send_Data(&huart4,(uint8_t*)VB_cmd,sizeof(VB_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x02://振动位移
				{
					uint8_t VB_cmd[]={0x50,0x03,0x00,0x41,0x00,0x03,0x58,0x5E};
					Send_Data(&huart4,(uint8_t*)VB_cmd,sizeof(VB_cmd));
					timer_count = 1;//计时开始
					break;
				}
				case 0x01://振动频率
				{
					uint8_t VB_cmd[]={0x50,0x03,0x00,0x44,0x00,0x03,0x48,0x5F};
					Send_Data(&huart4,(uint8_t*)VB_cmd,sizeof(VB_cmd));
					timer_count = 1;//计时开始
					break;
				}
				default:
					break;
			}
		}
		VB_collect_send_step= 0;
	}
}

//	//读取测量距离
//	uint8_t data_cmd[]={0x01,0x03,0x00,0x0F,0x00,0x02,0xF4,0x08};
//	Send_Data(&huart4,(uint8_t*)data_cmd,sizeof(data_cmd));
////	HAL_Delay(500);
//	if(nbiot_rx_flag)//收到返回的数据
//	{
//		nbiot_rx_flag = 0;//标志位清一下
//		NB_receivedata_CRC(nbiot_rx_buffer,nbiot_rx_len);//数据检查
//	}
//}



//数据检查
void NB_receivedata_CRC(uint8_t *rx_data,uint8_t len)
{
	char buffer[20];
	//	uint16_t m_crc = 0,c_crc = 0;//用于存储计算出的CRC和接收到的CRC值的变量
	if((rx_data[0]==0x50)&&(rx_data[1]==0x03))//modbus地址50，功能码03
	{
	//		m_crc = Computer_CRC16(rx_data,len - 2);		 //计算CRC
	//		c_crc = (*(rx_data + len - 2)<<8)|(*(rx_data + len - 1));//CRC校验值  倒数1 2位
	//		if(m_crc == c_crc)
	//		{
		switch (VB_collect_step)
		{
			case 0x06:
				rx_data[1]=0x34;//加速度
				break;
			case 0x05:
				rx_data[1]=0x37;//角速度
				break;
			case 0x04:
				rx_data[1]=0x3A;//振动速度
				break;
			case 0x03:
				rx_data[1]=0x3D;//振动角度
				break;
			case 0x02:
				rx_data[1]=0x41;//振动位移
				break;
			case 0x01:
				rx_data[1]=0x44;//振动频率
				break;
		}
		Send_Data(&huart3, rx_data, sizeof(rx_data));
	}
}
////读取振动传感器指令
//void VB01_cmd(void)
//{
//
//	//读取三轴加速度指令
//	//data_cmd[]={0x50,0x03,0x00,0x34,0x00,0x03,0x49,0x84};
//	Send_Data(&huart5,(uint8_t*)data_cmd,sizeof(data_cmd));
//	HAL_Delay(500);
//	if(lora_rx_flag)//收到返回的数据
//	{
//		lora_rx_flag = 0;//标志位清一下
//		lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);//数据检查
//	}
//
//	//读取三轴角速度指令
//	//data_cmd[]={0x50,0x03,0x00,0x37,0x00,0x03,0xB9,0x84};
//	data_cmd[3] = 0x37;
//	data_cmd[6] = 0xB9;
//	data_cmd[7] = 0x84;
//	Send_Data(&huart5,(uint8_t*)data_cmd,sizeof(data_cmd));
//	HAL_Delay(250);
//	if(lora_rx_flag)//收到返回的数据
//	{
//		lora_rx_flag = 0;//标志位清一下
//		lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);
//	}
//
//	//读取三轴振动速度指令
//	//data_cmd[]={0x50,0x03,0x00,0x3A,0x00,0x03,0x28,0x47};
//	data_cmd[3] = 0x3A;
//	data_cmd[6] = 0x28;
//	data_cmd[7] = 0x47;
//	Send_Data(&huart5,(uint8_t*)data_cmd,sizeof(data_cmd));
//	HAL_Delay(250);
//	if(lora_rx_flag)//收到返回的数据
//	{
//		lora_rx_flag = 0;//标志位清一下
//		lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);
//	}
//
//	//读取三轴振动角度指令
//	//data_cmd[]={0x50,0x03,0x00,0x3D,0x00,0x03,0x99,0x86};
//	data_cmd[3] = 0x3D;
//	data_cmd[6] = 0x99;
//	data_cmd[7] = 0x86;
//	Send_Data(&huart5,(uint8_t*)data_cmd,sizeof(data_cmd));
//	HAL_Delay(100);
//	if(lora_rx_flag)//收到返回的数据
//	{
//		lora_rx_flag = 0;//标志位清一下
//		lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);
//	}
//
//	//读取三轴振动位移指令
//	//data_cmd[]={0x50,0x03,0x00,0x3D,0x00,0x03,0x99,0x86};
//	data_cmd[3] = 0x41;
//	data_cmd[6] = 0x58;
//	data_cmd[7] = 0x3E;
//	Send_Data(&huart5,(uint8_t*)data_cmd,sizeof(data_cmd));
//	HAL_Delay(250);
//	if(lora_rx_flag)//收到返回的数据
//	{
//		lora_rx_flag = 0;//标志位清一下
//		lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);
//	}
//
//	//读取三轴振动频率指令
//	//data_cmd[]={0x50,0x03,0x00,0x3D,0x00,0x03,0x99,0x86};
//	data_cmd[3] = 0x44;
//	data_cmd[6] = 0x48;
//	data_cmd[7] = 0x5F;
//	Send_Data(&huart5,(uint8_t*)data_cmd,sizeof(data_cmd));
//	HAL_Delay(100);
//	if(lora_rx_flag)//收到返回的数据
//	{
//		lora_rx_flag = 0;//标志位清一下
//		lora_receivedata_CRC(lora_rx_buffer,lora_rx_len);
//	}
//}
//收到振动传感器的数据，并校验
//void VB01data_CRC(uint8_t *rx_data,uint8_t len)
//{
////	 char buffer[20];
////	uint16_t m_crc = 0,c_crc = 0;//用于存储计算出的CRC和接收到的CRC值的变量
//	if((rx_data[0]==0x50)&&(rx_data[1]==0x03))//modbus地址50，功能码03
//	{
////		m_crc = Computer_CRC16(rx_data,len - 2);		 //计算CRC
////		c_crc = (*(rx_data + len - 2)<<8)|(*(rx_data + len - 1));//CRC校验值  倒数1 2位
////		if(m_crc == c_crc)
////		{
//			switch (data_cmd[3])
//			{
//				case 0x34:
//					rx_data[1]=0x34;//加速度
//					break;
//				case 0x37:
//					rx_data[1]=0x37;//角速度
//					break;
//				case 0x3A:
//					rx_data[1]=0x3A;//振动速度
//					break;
//				case 0x3D:
//					rx_data[1]=0x3D;//振动角度
//					break;
//				case 0x41:
//					rx_data[1]=0x41;//振动位移
//					break;
//				case 0x44:
//					rx_data[1]=0x44;//振动频率
//					break;
//			}
////			sprintf(buffer, "rx_data:%u", (unsigned int)rx_data);
////			printf("%s\n", buffer);//把收到的数据打印出来
////
////		}
//		Send_Data(&huart3, rx_data, sizeof(rx_data));
//	}
//}
#endif
